package code.google.jcontainer.event;

import java.util.HashSet;
import java.util.Set;

/**
 * Used to manage listeners and fire events
 *
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
public class EventManager {

    private String name;
    private final Set<ContainerListener> listeners = new HashSet<ContainerListener>();

    public EventManager(String name) {
        this.name = name;
    }

    /**
     * return EventManager name, equals Container name 
     */
    public String getName() {
        return name;
    }

    public void addListener(ContainerListener listener) {
        listeners.add(listener);
    }

    public void removeListener(ContainerListener listener) {
        listeners.remove(listener);
    }

    public void fireComponentMethodInvokeEvent(ComponentInvocationEvent componentInvocationEvent) {
        for(ContainerListener listener : listeners) {
            listener.onComponentMethodInvoke(componentInvocationEvent);
        }
    }

    public void fireComponentMethodReturnEvent(ComponentInvocationEvent componentInvocationEvent) {
        for(ContainerListener listener : listeners) {
            listener.onComponentMethodReturn(componentInvocationEvent);
        }
    }

}
